<template>
  <div>
    <el-collapse-transition>
      <card-search v-if="searchCardShow">
        <el-form ref="form" :model="form" label-width="80px" :inline="true">
          <el-form-item label="任务分组">
            <el-select
              v-model="value"
              placeholder="请选择"
              style="width: 250px"
              clearable
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="任务状态">
            <el-select
              v-model="value"
              placeholder="请选择"
              style="width: 250px"
              clearable
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="执行时间">
            <el-select
              v-model="value"
              placeholder="请选择"
              style="width: 250px"
              clearable
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button
              class="btn-search"
              round
              icon="el-icon-search"
              size="medium"
              >搜索</el-button
            >
            <el-button
              class="btn-search"
              round
              icon="el-icon-refresh"
              size="medium"
              >重置</el-button
            >
          </el-form-item>
        </el-form>
      </card-search>
    </el-collapse-transition>
    <card-table style="height: calc(100vh - 100px)">
      <div class="card-table-header">
        <div class="card-table-header-btns">
          <el-button
            v-if="isSuperAdmin || hasPermission('monitor:job:add')"
            class="btn-search"
            size="small"
            icon="el-icon-edit"
            @click="handleClick(null, 'add')"
            >新增</el-button
          >
          <el-button
            v-if="isSuperAdmin || hasPermission('monitor:job:edit')"
            class="btn-search"
            size="small"
            icon="el-icon-edit"
            :disabled="btnDisabled"
            @click="handleClick(null, 'change')"
            >编辑</el-button
          >

          <el-button
            v-if="isSuperAdmin || hasPermission('monitor:job:remove')"
            class="btn-search"
            size="small"
            icon="el-icon-delete"
            :disabled="btnDisabled"
            @click="handleClick(null, 'delete')"
            >删除</el-button
          >
          <el-button
            class="btn-search"
            size="small"
            icon="el-icon-upload2"
            v-if="isSuperAdmin || hasPermission('monitor:job:export')"
            >导出</el-button
          >
          <!-- <el-button
            class="btn-search"
            size="small"
            @click="handleClick(null, 'cron')"
            ><svg-icon icon-class="fx" style="padding-right: 5px"></svg-icon
            >生成表达式</el-button
          > -->
          <el-button
            v-if="isSuperAdmin || hasPermission('monitor:job:detail')"
            class="btn-search"
            size="small"
            icon="el-icon-document"
            @click="handleClick(null, 'log')"
            >日志</el-button
          >
          <el-input
            v-if="isSuperAdmin || hasPermission('monitor:job:list')"
            v-model="input"
            placeholder="请输入任务名称"
            style="width: 450px; margin-left: 10px"
            clearable
            size="small"
          >
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </div>
        <div class="card-table-header-actions">
          <table-action
            :table-header="tableHeader"
            :eye-show="false"
            :search-show="false"
            @check-change="handleCheckChange"
            @search-click="handleSearchClick"
            @init-click="handleInitClick"
          />
        </div>
      </div>
      <el-table
        :key="tableKey"
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        :header-cell-style="{ background: '#eff3f8', color: '#333' }"
        size="small"
        stripe
        :default-sort="{ prop: 'date', order: 'descending' }"
        :max-height="tableHeight"
        @selection-change="handleSelectionChange"
        highlight-selection-row
        border
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          v-for="item in tableHeader"
          v-if="item.visible"
          :key="item.prop"
          :label="item.title"
          :min-width="item.width"
          :prop="item.prop"
          :show-overflow-tooltip="true"
          :align="item.align"
          sortable
        >
          <template #default="scope">
            <span v-if="item.prop === 'status'" class="link-type">
              <el-switch
                v-model="scope.row[item.prop]"
                size="small"
                :width="35"
            /></span>
            <span v-else>
              {{ scope.row[item.prop] }}
            </span>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="300">
          <template slot-scope="scope">
            <el-button
              type="primary"
              @click="handleClick(scope.row, 'execute')"
              size="mini"
              icon="el-icon-video-play"
              >执行一次</el-button
            >
            <el-button
              v-if="isSuperAdmin || hasPermission('monitor:job:detail')"
              type="primary"
              size="mini"
              icon="el-icon-tickets"
              @click="handleClick(scope.row, 'detail')"
              >任务详情</el-button
            >
            <el-button
              v-if="isSuperAdmin || hasPermission('monitor:job:detail')"
              type="primary"
              size="mini"
              icon="el-icon-reading"
              @click="handleClick(scope.row, 'log')"
              >调度日志</el-button
            >
          </template></el-table-column
        >
      </el-table>
      <XyPagination
        :total="total"
        :pageSizeNum="pageSize"
        :pageNum="currentPage"
        @pagination="handlerPagination"
      ></XyPagination>
    </card-table>

    <CronExpression
      v-model="dialogCronVisible"
      :field="field"
      @close="handleDialogChangeClose"
    />
    <AddTask
      v-model="dialogDetailVisible"
      :field="field"
      @close="handleDialogDetailClose"
    />
    <LogDetail
      v-model="dialogLogVisible"
      :field="field"
      @close="handleDialogLogClose"
    />
    <TaskDetail
      v-model="dialogTaskDetailVisible"
      :field="field"
      @close="handleDialogTaskClose"
    ></TaskDetail>
  </div>
</template>
<script>
import CardSearch from "@/components/CardSearch";
import CardTable from "@/components/CardTable";
import TableAction from "@/components/TableAction";
import AddTask from "./components/DialogAddTask.vue";
import CronExpression from "./components/DialogCronExpression.vue";
import LogDetail from "./components/DialogLog.vue";
import TaskDetail from "./components/DialogTaskDetail.vue";
import XyPagination from "@/components/XyPagination";
import { hasPermission, isSuperAdmin } from "@/utils/hasBtnPermission";
export default {
  components: {
    CardSearch,
    CardTable,
    TableAction,
    AddTask,
    CronExpression,
    LogDetail,
    TaskDetail,
    XyPagination,
  },
  data() {
    return {
      tableKey: 0, // 用于强制刷新表格
      searchCardShow: false,
      input: "",
      form: {},
      tableHeader: [
        {
          title: "任务名称",
          prop: "projectName",
          width: "150",
          visible: true,
          align: "left",
        },
        {
          title: "任务分组",
          prop: "projectAdmin",
          width: "120",
          visible: true,
          align: "center",
        },
        {
          title: "调用目标字符串",
          prop: "company",
          width: "150",
          visible: true,
          align: "left",
        },
        {
          title: "执行表达式",
          prop: "totleNum",
          width: "150",
          visible: true,
          align: "center",
        },
        {
          title: "任务状态",
          prop: "status",
          width: "80",
          visible: true,
          align: "center",
        },
        {
          title: "创建时间",
          prop: "date",
          width: "150",
          visible: true,
          align: "center",
        },
      ],
      tableData: [
        {
          date: "2016-05-03",
          updateTime: "2016-05-03",
          name: "王小虎",
          projectName: "久诺新能源",
          projectAdmin: "13204053226",
          company: "鑫源直属项目管理平台",
          totleNum: "13654079988",
          status: true,
          personInCharge: "普通用户",
          address: "上海市普陀区金沙江路 1518 弄",
        },
      ],
      multipleSelection: [],
      btnDisabled: true,
      currentPage: 1,
      pageSize: 20,
      tableHeight: 550,
      total: 0,
      dialogDetailVisible: false,
      dialogCronVisible: false,
      dialogLogVisible: false,
      field: "",
      dialogTaskDetailVisible: false,
      options: [
        {
          value: "选项1",
          label: "黄金糕",
        },
        {
          value: "选项2",
          label: "双皮奶",
        },
        {
          value: "选项3",
          label: "蚵仔煎",
        },
        {
          value: "选项4",
          label: "龙须面",
        },
        {
          value: "选项5",
          label: "北京烤鸭",
        },
      ],
      value: "",
    };
  },
  computed: {
    isSuperAdmin() {
      return isSuperAdmin();
    },
  },
  mounted() {
    this.calculateTableHeight();
    this.initTable();
  },
  methods: {
    initTable() {},
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      if (this.multipleSelection.length > 0) {
        this.btnDisabled = false;
      } else {
        this.btnDisabled = true;
      }
      console.log(this.multipleSelection);
    },
    handleClick(row, field) {
      switch (field) {
        case "change":
          this.dialogDetailVisible = true;
          this.field = "修改任务";
          break;
        case "add":
          this.dialogDetailVisible = true;
          this.field = "添加任务";
          break;
        case "cron":
          this.dialogCronVisible = true;
          this.field = "Cron表达式生成器";
          break;
        case "log":
          this.field = "调度日志";
          this.dialogLogVisible = true;
          break;
        case "detail":
          this.field = "任务详情";
          this.dialogTaskDetailVisible = true;
          break;
        case "delete":
          this.$confirm(
            `确认删除选中的${this.multipleSelection.length}个定时任务吗?`,
            "系统提示",
            {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning",
            }
          ).then(() => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          });
          break;
        default:
          break;
      }
    },
    // 处理表头显示隐藏
    handleCheckChange(checkedValues) {
      this.tableHeader.forEach((item) => {
        item.visible = checkedValues.includes(item.prop);
      });
      this.tableKey += 1;
    },
    handleSearchClick() {
      this.searchCardShow = !this.searchCardShow;
    },
    handleInitClick() {
      this.initTable();
    },
    handleDialogTaskClose() {
      this.field = "";
      this.dialogTaskDetailVisible = false;
    },
    handleDialogDetailClose() {
      this.field = "";
      this.dialogDetailVisible = false;
    },
    handleDialogChangeClose() {
      this.field = "";
      this.dialogCronVisible = false;
    },
    handleDialogLogClose() {
      this.field = "";
      this.dialogLogVisible = false;
    },
    handlerPagination(page) {
      this.currentPage = page.pageNum;
      this.pageSize = page.pageSize;
      this.initTable();
    },
    hasPermission(permission) {
      return hasPermission(permission);
    },
    calculateTableHeight() {
      // 根据容器实际高度计算表格高度
      this.$nextTick(() => {
        const container = this.$refs.multipleTable.$el?.parentElement;
        if (container) {
          // 减去表头和其他元素的高度
          this.tableHeight = container.clientHeight - 120; // 120为估计的其他元素高度
        }
      });
    },
  },
};
</script>
